package com.platform.admin.controller;

import com.platform.admin.pojo.req.CommonReq;
import com.platform.admin.pojo.req.SaveMenuReq;
import com.platform.admin.pojo.vo.SysMenuSelectVO;
import com.platform.admin.pojo.vo.SysMenuVO;
import com.platform.admin.service.ISysMenuService;
import com.platform.common.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Api(tags = "菜单控制器API")
@RestController
@RequestMapping("/menu")
@Slf4j
@RequiredArgsConstructor(onConstructor_=@Autowired)
public class MenuController {
    private final ISysMenuService sysMenuService;

    @ApiOperation("菜单详情")
    @GetMapping("/{id}")
    public R<SysMenuVO> getDetail(@PathVariable Long id) {
        return R.ok(sysMenuService.getDetail(id));
    }

    @ApiOperation("获取菜单数据列表")
    @GetMapping("/list")
    public R<List<SysMenuVO>> list() {
        return R.ok(sysMenuService.loadMenus());
    }

    @ApiOperation("创建菜单")
    @PostMapping
    public R createMenu(@Validated @RequestBody SaveMenuReq req) {
        sysMenuService.createMenu(req);
        return R.ok();
    }

    @ApiOperation("菜单树")
    @GetMapping("/listTree")
    public R<List<SysMenuVO>> listTree() {
        return R.ok(sysMenuService.listTree());
    }

    @ApiOperation("更新菜单")
    @PutMapping
    public R updateMenu(@Validated @RequestBody SaveMenuReq req) {
        sysMenuService.updateMenu(req);
        return R.ok();
    }

    @ApiOperation("更新菜单")
    @PatchMapping("/{id}/{status}")
    public R updateMenuStatus(@PathVariable Long id,@PathVariable int status) {
        sysMenuService.updateMenuStatus(id,status);
        return R.ok();
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("/{ids}")
    public R deletes(@PathVariable List<Long> ids) {
        sysMenuService.deletes(ids);
        return R.ok();
    }

    @ApiOperation("菜单select")
    @GetMapping("/select/{status}")
    public R<List<SysMenuSelectVO>> select(@PathVariable int status) {
        return R.ok(sysMenuService.select(status));
    }

    @ApiOperation("查询角色绑定的菜单")
    @GetMapping("/role/{roleId}")
    public R<List<Long>> listRoleMenu(@PathVariable Long roleId) {
        return R.ok(sysMenuService.listRoleMenu(roleId));
    }

    @ApiOperation("更新角色绑定的菜单")
    @PutMapping("/role/{roleId}")
    public R updateRoleMenu(@PathVariable Long roleId,@RequestBody CommonReq req) {
        sysMenuService.updateRoleMenu(roleId,req);
        return R.ok();
    }

    @ApiOperation("获取当前用户绑定的菜单")
    @GetMapping("/currentUser")
    public R<List<Long>> currentUser(){
        return R.ok(sysMenuService.currentUser());
    }
}
